﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListTest
{
    class MyList<T>
    {
        private int count;
        private int capacity;
        private T[] array;



        public MyList(int capacity)
        {
            array = new T[capacity];
            this.capacity = capacity;
        }

        public void Add(T data)
        {
            if (capacity == count)
            {
                Re();
            }
            array[count] = data;
            count++;

        }

        public void Insert(int index, T data)
        {
            if(index<0 || index>count-1)
            {
                throw new ArgumentOutOfRangeException("索引超出范围");
            }

            if (capacity == count)
            {
                Re();
            }
            for (int i = count - 1; i >= index; i--)
            {
                array[i + 1] = array[i];
            }
            array[index] = data;
            count++;
        }

        public void RemoveAt(int index)
        {
            if (index < 0 || index > count - 1)
            {
                throw new ArgumentOutOfRangeException("索引超出范围");
            }

            for (int i = index; i < count - 1; i++)
            {
                array[i] = array[i + 1];
            }
            count--;
        }

        public int IndexOf(T data)
        {
            int i = 0;
            for (; i < count; i++)
            {
                if (data.Equals(array[i]))
                {
                    return i;
                }
            }
            return -1;
        }




        public int LastIndexOf(T data)
        {
            int i = count-1;
            for (; i >=0; i--)
            {
                if (data.Equals(array[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        public void Sort()
        {
            Array.Sort(array, 0, count);        // (要排序的数组，起始下标，参加排序的元素个数)
        }

        public void Re()  // 扩容
        {
            T[] tmp = new T[2*capacity];
            for(int i=0;i<capacity;i++)
            {
                tmp[i] = array[i];
            }
            array = tmp;
            capacity = 2 * capacity;
        }

        // 索引器
        public T this[int index]
        {
            get
            {
                if (index < 0 || index > count - 1)
                {
                    throw new ArgumentOutOfRangeException("索引超出范围");
                }

                return array[index];
            }
            set
            {
                if (index < 0 || index > count - 1)
                {
                    throw new ArgumentOutOfRangeException("索引超出范围");
                }

                array[index] = value;
            }
        }

        // 属性
        public int Count
        {
            get
            {
                return count;
            }
        }

        public int Capacity
        {
            get
            {
                return array.Length;
            }
        }
    }
}
